<?php

declare(strict_types=1);

namespace App\Job;

use Hyperf\AsyncQueue\Job;
use App\Utils\ServerUtil;
use App\Service\AdminLogService;
use App\Service\CouponService;
use Exception;

class CouponGenerateJob extends Job
{
    public $params;

    public function __construct($params)
    {
        // 这里最好是普通数据，不要使用携带 IO 的对象，比如 PDO 对象
        $this->params = $params;
    }

    public function handle()
    {
        // 根据参数处理具体逻辑
        // 通过具体参数获取模型等
        try{
            $insertArr=ServerUtil::arraySplit($this->params['insertArr'],5000);
            foreach($insertArr as $val){
                make(CouponService::class)->couponAdd($val);
            }
            // $container = \Hyperf\Utils\ApplicationContext::getContainer();
             //添加管理员操作日记
            make(AdminLogService::class)->addLog('红包券队列已完成编号batch_id:'.$this->params['batch_id']);
        }catch(\Throwable $t){
            ServerUtil::info('红包券队列异常',['param'=>$this->params,'res'=>$t->getMessage()]);
        }
    }
}